
#include <stdio.h>
#include "i2c.h"
#include "systick.h"
#include "adxl345.h"

#define SW_IIC   1

static void adxl345_write_reg(uint8_t reg,uint8_t data)
{
#if SW_IIC == 1
    swi2c_device_write_data(ADXL345_ADDR,&reg,1,&data,1);
#endif    
}

static void adxl345_read_reg(uint8_t reg,uint8_t *data)
{
#if SW_IIC == 1
    swi2c_device_read_data(ADXL345_ADDR,&reg,1,data,1);
#endif 
}

static void adxl345_get_x_y_z_data(uint8_t reg,uint8_t *data,uint16_t len)
{
#if SW_IIC == 1
    swi2c_device_read_data(ADXL345_ADDR,&reg,1,data,len);
#endif   
}

/**
 * 获取设备ID的函数
*/
static void adxl345_get_device_id(uint8_t *id)
{
    adxl345_read_reg(ADXL345_REG_DEVID,id);
}

/**
 * 设置中断是高电平还是低电平的函数
*/
static uint8_t adxl345_set_interrupt_active_level(adxl345_interrupt_active_level_t active_level)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT,&value);
    value &= ~(1 << 5);                                                                          /* clear config */
    value |= (active_level << 5); 
    delay_1ms(100);     
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT,value);

    return 0;
}

static uint8_t adxl345_get_interrupt_active_level(adxl345_interrupt_active_level_t *active_level)
{
    uint8_t value = 0;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT,&value);
    value &= (0x01<<5);
    *active_level = (adxl345_interrupt_active_level_t )(value >> 5);
    return 0;
}

static uint8_t adxl345_set_auto_sleep(adxl345_bool_t enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= ~(1 << 4);                                                                        /* clear config */
    value |= enable << 4;  
    delay_1ms(100);      
    adxl345_write_reg(ADXL345_REG_POWER_CTL,value);

    return 0;
}

static uint8_t adxl345_get_auto_sleep(adxl345_bool_t *enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= (0x01 << 4);
    *enable = (adxl345_bool_t )(value >> 4);

    return 0;
}

static uint8_t adxl345_set_sleep(adxl345_bool_t enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= ~(1 << 2);                                                                        /* clear config */
    value |= enable << 2;    
    delay_1ms(100);    
    adxl345_write_reg(ADXL345_REG_POWER_CTL,value);

    return 0;
}

static uint8_t adxl345_get_sleep(adxl345_bool_t *enable)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);

    value &= (0x01 << 2);                                                                        /* clear config */
    *enable = (adxl345_bool_t) (value >>2);
    return 0;
}

static uint8_t adxl345_set_sleep_frequency(adxl345_sleep_frequency_t sleep_frequency)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= ~(0x03 << 0);                                                                        /* clear config */
    value |= (sleep_frequency << 0);    
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_POWER_CTL,value);
    return 0;
}

static uint8_t adxl345_get_sleep_frequency(adxl345_sleep_frequency_t *sleep_frequency)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_POWER_CTL,&value);
    value &= (0x03 << 0);                                                                        /* clear config */
    *sleep_frequency = (adxl345_sleep_frequency_t) (value >> 0);
    return 0;
}

static uint8_t adxl345_set_fifo_mode(adxl345_mode_t fifo_mode)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= ~(0x03 << 6);                                                                       /* clear config */
    value |= (fifo_mode << 6); 
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_FIFO_CTL,value);
    return 0;
}

static uint8_t adxl345_get_fifo_mode(adxl345_mode_t *fifo_mode)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= (0x03 << 6);                                                                       /* clear config */
    *fifo_mode = (adxl345_mode_t) value >> 6;
    return 0;
}

static uint8_t adxl345_set_trigger_pin(adxl345_interrupt_pin_t pin)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= ~(0x01<< 5);                                                                       /* clear config */
    value |= (pin << 5); 
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_FIFO_CTL,value);

    return 0;
}

static uint8_t adxl345_get_trigger_pin(adxl345_interrupt_pin_t *pin)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= (0x01<< 5);                                                                       /* clear config */
    *pin = (adxl345_interrupt_pin_t) value >> 5;
    return 0;
}

static uint8_t adxl345_set_watermark(uint8_t level)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= ~(0x1F<< 0);                                                                       /* clear config */
    value |= (level << 0); 
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_FIFO_CTL,value);
    return 0;
}

static uint8_t adxl345_get_watermark(uint8_t *level)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_FIFO_CTL,&value);
    value &= (0x1F<< 0);                                                                       /* clear config */
    *level = value >> 0;
    return 0;
}

static uint8_t adxl345_offset_convert_to_register(float g, uint8_t *reg)
{
    *reg = (int8_t)(g / 0.0156f);    
    return 0;                                                                                   // 比例因子为15.6 mg/LSB(即0x7F = 2 g)。
}

static uint8_t adxl345_offset_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0156f;    
    return 0;
}

static uint8_t adxl345_set_offset(uint8_t x, uint8_t y, uint8_t z) 
{
    adxl345_write_reg(ADXL345_REG_OFSX,x);
    adxl345_write_reg(ADXL345_REG_OFSY,y);
    adxl345_write_reg(ADXL345_REG_OFSZ,z);

    return 0;
}

static uint8_t adxl345_get_offset(uint8_t *x, uint8_t *y, uint8_t *z) 
{
    adxl345_read_reg(ADXL345_REG_OFSX,x);
    adxl345_read_reg(ADXL345_REG_OFSY,y);
    adxl345_read_reg(ADXL345_REG_OFSZ,z);

    return 0;
}

static uint8_t adxl345_tap_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
    return 0;                             /* success return 0 */
}

static uint8_t adxl345_tap_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
    return 0;                           /* success return 0 */
}

static uint8_t adxl345_set_tap_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_TAP ,threshold);        /* write config */
    return 0;
}

static uint8_t adxl345_get_tap_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_TAP ,threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_duration_convert_to_register(uint32_t us, uint8_t *reg)
{
    *reg = (uint8_t)(us / 625);        /* convert real data to register data */
    return 0;                          /* success return 0 */
}

static uint8_t adxl345_duration_convert_to_data(uint8_t reg, uint32_t *us)
{
    *us = (uint32_t)((float)(reg) * 625.0f);        /* convert raw data to real data */
    return 0;                                       /* success return 0 */
}

static uint8_t adxl345_set_duration(uint8_t t) 
{
    adxl345_write_reg(ADXL345_REG_DUR, t);          /* write config */
    return 0;
}

static uint8_t adxl345_get_duration(uint8_t *t) 
{
    adxl345_read_reg(ADXL345_REG_DUR, t);          /* write config */
    return 0;
}

static uint8_t adxl345_latent_convert_to_register(float ms, uint8_t *reg)
{
    *reg = (uint8_t)(ms / 1.25f);        /* convert real data to register data */
    return 0;                            /* success return 0 */
}

static uint8_t adxl345_latent_convert_to_data(uint8_t reg, float *ms)
{
    *ms = (float)(reg) * 1.25f;        /* convert raw data to real data */
    return 0;                          /* success return 0 */
}

static uint8_t adxl345_set_latent(uint8_t t) 
{
    adxl345_write_reg(ADXL345_REG_LATENT, t);       /* write config */
    return 0;
}

static uint8_t adxl345_get_latent(uint8_t *t) 
{
    adxl345_read_reg(ADXL345_REG_LATENT, t);       /* write config */
    return 0;
}

static uint8_t adxl345_window_convert_to_register(float ms, uint8_t *reg)
{
    *reg = (uint8_t)(ms / 1.25f);        /* convert real data to register data */
    return 0;                            /* success return 0 */
}

static uint8_t adxl345_window_convert_to_data(uint8_t reg, float *ms)
{
    *ms = (float)(reg) * 1.25f;        /* convert raw data to real data */
    return 0;                          /* success return 0 */
}

uint8_t adxl345_set_window(uint8_t t)
{
    adxl345_write_reg(ADXL345_REG_WINDOW, t);       /* write config */
    return 0;
}

uint8_t adxl345_get_window(uint8_t *t)
{
    adxl345_read_reg(ADXL345_REG_WINDOW, t);       /* write config */
    return 0;
}

static uint8_t adxl345_set_tap_axis(adxl345_tap_axis_t axis, adxl345_bool_t enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                        /* read config */
 
    prev &= ~(1 << axis);                                                                   /* clear axis */
    prev |= enable << axis;                                                                 /* set axis */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_TAP_AXES, prev);             /* write config */
    return 0;
}

static uint8_t adxl345_get_tap_axis(adxl345_tap_axis_t axis, adxl345_bool_t *enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                                /* read config */  
    prev &= (1 << axis);                                                                   /* clear axis */
    *enable = (adxl345_bool_t)(prev >> axis);                                                            /* set axis */
    
    return 0;
}

static uint8_t adxl345_set_tap_suppress(adxl345_bool_t enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                        /* read config */
 
    prev &= ~(1 << 3);                                                                       /* clear suppress */
    prev |= enable << 3;                                                                   /* set axis */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_TAP_AXES, prev);             /* write config */
    return 0;
}

static uint8_t adxl345_get_tap_suppress(adxl345_bool_t *enable)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_TAP_AXES,(uint8_t *)&prev);                                /* read config */  
    prev &= (1 << 3);                                                                      /* clear config */
    *enable = (adxl345_bool_t)(prev >> 3);   

    return 0;
}

static uint8_t adxl345_set_interrupt_map(adxl345_interrupt_t type, adxl345_interrupt_pin_t pin)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_MAP, (uint8_t *)&prev);                                /* read config */

    prev &= ~(1 << type);                                                                   /* clear type */
    prev |= pin << type;  
    delay_1ms(100);            
    adxl345_write_reg(ADXL345_REG_INT_MAP, prev);                                          /* write config */
    return 0;
}

static uint8_t adxl345_get_interrupt_map(adxl345_interrupt_t type, adxl345_interrupt_pin_t *pin)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_MAP, (uint8_t *)&prev);                                /* read config */
    prev &= (1 << type);                                                                    /* clear config */
    *pin = (adxl345_interrupt_pin_t)(prev >> type);   

    return 0;
}

static uint8_t adxl345_set_interrupt(adxl345_interrupt_t type, adxl345_bool_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_ENABLE, (uint8_t *)&prev);                              /* read config */
 
    prev &= ~(1 << type);                                                                       /* clear interrupt */
    prev |= enable << type; 
    delay_1ms(100);  
                                                                                    /* set interrupt */
    adxl345_write_reg(ADXL345_REG_INT_ENABLE, prev);                             /* write config */

    return 0;
}

static uint8_t adxl345_get_interrupt(adxl345_interrupt_t type, adxl345_bool_t *enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_INT_ENABLE, (uint8_t *)&prev);                              /* read config */
    
    prev &= (1 << type);                                                                     /* clear config */
    *enable = (adxl345_bool_t)(prev >> type);    

    return 0;
}

static uint8_t adxl345_set_link_activity_inactivity(adxl345_bool_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);                                  /* read config */
 
    prev &= ~(1 << 5);                                                                          /* clear config */
    prev |= enable << 5;    
    delay_1ms(100);                                                                    /* set interrupt */
    adxl345_write_reg(ADXL345_REG_POWER_CTL, prev);                                             /* write config */

    return 0;

}

static uint8_t adxl345_get_link_activity_inactivity(adxl345_bool_t *enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);                                  /* read config */
 
    prev &= 1 << 5;                                                                             /* get config */
    *enable = (adxl345_bool_t)(prev >> 5);   

    return 0;

}

static uint8_t adxl345_action_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
    return 0;                             /* success return 0 */
}

static uint8_t adxl345_action_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
    return 0;                           /* success return 0 */
}

static uint8_t adxl345_set_action_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_ACT, threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_get_action_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_ACT, threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_inaction_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
    return 0;                             /* success return 0 */
}

static uint8_t adxl345_inaction_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
    return 0;                           /* success return 0 */
}

static uint8_t adxl345_set_inaction_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_INACT, threshold);       /* write config */
    return 0;
}

static uint8_t adxl345_get_inaction_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_INACT, threshold);       /* read config */
    return 0;
}

static uint8_t adxl345_inaction_time_convert_to_register(uint8_t s, uint8_t *reg)
{
    *reg = s;                       /* convert real data to register data */
    return 0;                       /* success return 0 */
}

static uint8_t adxl345_inaction_time_convert_to_data(uint8_t reg, uint8_t *s)
{
    *s = reg;                       /* convert raw data to real data */
    return 0;                       /* success return 0 */
}

static void adxl345_set_inaction_time(uint8_t t)
{
    adxl345_write_reg(ADXL345_REG_TIME_INACT, t);       /* write config */
}

static void adxl345_get_inaction_time(uint8_t *t)
{
    adxl345_read_reg(ADXL345_REG_TIME_INACT, t);       /* write config */
}

static void adxl345_set_action_inaction(adxl345_action_inaction_t type, adxl345_bool_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);          /* read config */

    prev &= ~(1 << type);                                                                         /* clear type */
    prev |= (enable << type);                                                                     /* set type */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_ACT_INACT_CTL,prev);                          /* write config */
}

static void adxl345_get_action_inaction(adxl345_action_inaction_t type, adxl345_bool_t *enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= (1 << type);                                                                        /* get type */
    *enable = (adxl345_bool_t)(prev >> type);                                                   /* set type */                                                                                /* success return 0 */
}

static void adxl345_set_action_coupled(adxl345_coupled_t coupled)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);         /* read config */
 
    prev &= ~(1 << 7);                                                                            /* clear coupled */
    prev |= (coupled << 7);                                                                       /* set coupled */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_ACT_INACT_CTL,prev);       /* write config */
}

static void adxl345_get_action_coupled(adxl345_coupled_t *coupled)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= (1 << 7);                                                                           /* clear config */
    *coupled = (adxl345_coupled_t)(prev >> 7);                                                  /* set config */                                                                               /* success return 0 */
}

static void adxl345_set_inaction_coupled(adxl345_coupled_t coupled)
{
    uint8_t prev;

    
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= ~(1 << 3);                                                                          /* clear config */
    prev |= (coupled << 3);                                                                     /* set inaction coupled */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_ACT_INACT_CTL, prev);     /* write config */
}

static void adxl345_get_inaction_coupled(adxl345_coupled_t *coupled)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_ACT_INACT_CTL, (uint8_t *)&prev);       /* read config */

    prev &= (1 << 3);                                                                           /* clear config */
    *coupled = (adxl345_coupled_t)(prev >> 3);                                                  /* set config */                                                                               /* success return 0 */
}

static void adxl345_free_fall_threshold_convert_to_register(float g, uint8_t *reg)
{
    *reg = (uint8_t)(g / 0.0625f);        /* convert real data to register data */
}

static void adxl345_free_fall_threshold_convert_to_data(uint8_t reg, float *g)
{
    *g = (float)(reg) * 0.0625f;        /* convert raw data to real data */
}

static void adxl345_set_free_fall_threshold(uint8_t threshold)
{
    adxl345_write_reg(ADXL345_REG_THRESH_FF, threshold);       /* write config */
}

static void adxl345_get_free_fall_threshold(uint8_t *threshold)
{
    adxl345_read_reg(ADXL345_REG_THRESH_FF, threshold);       /* write config */
}

static void adxl345_free_fall_time_convert_to_register(uint16_t ms, uint8_t *reg)
{
    *reg = (uint8_t)(ms / 5);        /* convert real data to register data */
}

static void adxl345_free_fall_time_convert_to_data(uint8_t reg, uint16_t *ms)
{
    *ms = reg * 5;                  /* convert raw data to real data */
}

static void adxl345_set_free_fall_time(uint8_t t)
{
    adxl345_write_reg(ADXL345_REG_TIME_FF, t);       /* write config */
}

static void adxl345_get_free_fall_time(uint8_t *t)
{
    adxl345_read_reg(ADXL345_REG_TIME_FF, t);       /* write config */
}

static void adxl345_set_justify(adxl345_justify_t enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);         /* read config */
    prev &= ~(1 << 2);                                                                          /* clear config */
    prev |= (enable << 2);                                                                      /* set justify */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT, prev);       /* write config */
}

static void adxl345_get_justify(adxl345_justify_t *enable)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);                                /* read config */

    prev &= (1 << 2);                                                                         /* get config */
    *enable = (adxl345_justify_t)(prev >> 2);                                                 /* get justify */
}

static void adxl345_set_rate(adxl345_rate_t rate)
{
    uint8_t prev;
    
    adxl345_read_reg(ADXL345_REG_BW_RATE, (uint8_t *)&prev);                                    /* read config */

    prev &= ~(0x1F);                                                                            /* clear rate */
    prev |= rate;                                                                               /* set rate */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_BW_RATE, prev);                                               /* write config */
}

static void adxl345_get_rate(adxl345_rate_t *rate)
{
    uint8_t prev;

    adxl345_read_reg(ADXL345_REG_BW_RATE, (uint8_t *)&prev);                            /* read config */

    prev &= 0x1F;                                                                         /* clear config */
    *rate = (adxl345_rate_t)(prev);                                                       /* get rate */                                                                          /* success return 0 */
}

static void adxl345_set_full_resolution(adxl345_bool_t enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);         /* read config */
 
    prev &= ~(1 << 3);                                                                          /* clear resolution */
    prev |= (enable << 3);  
    delay_1ms(100);                                                                 /* set resolution */
    
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT, prev );       /* write config */
}

static void adxl345_get_full_resolution(adxl345_bool_t *enable)
{
    uint8_t  prev;

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);       /* read config */
    prev &= (1 << 3);                                                                         /* clear config */
    *enable = (adxl345_bool_t)(prev >> 3);                                                    /* set resolution */                                                                            /* success return 0 */
}

static void adxl345_set_range(adxl345_range_t range)
{
    uint8_t res, prev;
    

    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);         /* read config */

    prev &= ~(3 << 0);                                                                          /* clear config */
    prev |= (range << 0);                                                                       /* set range */
    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_DATA_FORMAT, prev);       /* write config */
}

static void adxl345_get_range(adxl345_range_t *range)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_DATA_FORMAT, (uint8_t *)&prev);       /* read config */
    prev &= (3 << 0);                                                                         /* get config */
    *range = (adxl345_range_t)(prev >> 0);                                                    /* set range */
}

static void adxl345_set_measure(adxl345_bool_t enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);                                  /* read config */

    prev &= ~(1 << 3);                                                                          /* clear config */
    prev |= enable << 3;                                                                        /* set measure */

    delay_1ms(100);
    adxl345_write_reg(ADXL345_REG_POWER_CTL,prev);                                              /* write config */
}

static void adxl345_get_measure(adxl345_bool_t *enable)
{
    uint8_t prev;
    adxl345_read_reg(ADXL345_REG_POWER_CTL, (uint8_t *)&prev);       /* read config */
    prev &= 1 << 3;                                                                         /* get config */
    *enable = (adxl345_bool_t)(prev >> 3);                                                  /* get measure */
}

void adxl345_xyz_read_init()
{
    uint8_t id = 0;
    uint8_t reg;
    uint8_t watermark;
    uint8_t x_offset;
    uint8_t y_offset;
    uint8_t z_offset;
    uint8_t tap_threshold;
    uint8_t tap_duration;
    uint8_t tap_window;
    uint8_t latent;
    uint8_t action_threshold;
    uint8_t inaction_threshold;
    uint8_t inaction_time;
    uint8_t fall_threshold;
    uint8_t free_fall_time;
    adxl345_interrupt_active_level_t it_level;
    adxl345_bool_t auto_sleep_enable;
    adxl345_bool_t sleep_enable;
    adxl345_sleep_frequency_t sleep_freq;
    adxl345_mode_t fifo_mode;
    adxl345_interrupt_pin_t interrupt_pin;

    adxl345_get_device_id(&id);
    if (id == 0xe5)
    {
        adxl345_set_interrupt_active_level(ADXL345_INTERRUPT_ACTIVE_LEVEL_LOW);     // 设置中断为低电平
        adxl345_get_interrupt_active_level(&it_level);
        printf("adxl345_get_interrupt_active_level == 0x%02x\r\n",it_level);

        adxl345_set_auto_sleep(ADXL345_BOOL_FALSE);                                 // 设置自动休眠为关
        adxl345_get_auto_sleep(&auto_sleep_enable);
        printf("adxl345_get_auto_sleep == 0x%02x\r\n",auto_sleep_enable);

        adxl345_set_sleep(ADXL345_BOOL_FALSE);                                      // 置为0，普通工作模式，置为1 ，休眠模式
        adxl345_get_sleep(&sleep_enable);
        printf("adxl345_get_sleep == 0x%02x\r\n",sleep_enable);

        adxl345_set_sleep_frequency(ADXL345_SLEEP_FREQUENCY_1HZ);
        adxl345_get_sleep_frequency(&sleep_freq);
        printf("adxl345_get_sleep_frequency == 0x%02x\r\n",sleep_freq);

        adxl345_set_fifo_mode(ADXL345_MODE_BYPASS);
        adxl345_get_fifo_mode(&fifo_mode);
        printf("adxl345_get_fifo_mode == 0x%02x\r\n",fifo_mode);

        adxl345_set_trigger_pin(ADXL345_INTERRUPT_PIN1);
        adxl345_get_trigger_pin(&interrupt_pin);
        printf("adxl345_get_trigger_pin == 0x%02x\r\n",interrupt_pin);

        adxl345_set_watermark(0x1F);
        adxl345_get_watermark(&watermark);
        printf("adxl345_get_watermark == 0x%02x\r\n",watermark);

        //set x y z offset
        adxl345_offset_convert_to_register(0.0f,&reg);
        adxl345_set_offset(reg,reg,reg);
        adxl345_get_offset((uint8_t *)&x_offset,(uint8_t *)&y_offset,(uint8_t *)&z_offset);
        printf("x_offset == 0x%02x y_offset == 0x%02x z_offset= 0x%02x\r\n",x_offset,y_offset,z_offset);

        /* set threshold 3g */
        adxl345_tap_threshold_convert_to_register(3.0f,&reg);
        /* set tap threshold */
        adxl345_set_tap_threshold(reg);
        adxl345_get_tap_threshold(&tap_threshold);
        printf("adxl345_get_tap_threshold == 0x%02x\r\n",tap_threshold);

        /* set 10 ms */
        adxl345_duration_convert_to_register(10 * 1000, (uint8_t *)&reg);
        /* set duration */
        adxl345_set_duration(reg);
        adxl345_get_duration(&tap_duration);
        printf("adxl345_get_duration == 0x%02x\r\n",tap_duration);

        /* set 20 ms */
        adxl345_latent_convert_to_register(20.0f, (uint8_t *)&reg);
        /* set latent */
        adxl345_set_latent(reg);
        adxl345_get_latent(&latent);
        printf("adxl345_get_latent == 0x%02x\r\n",latent);

        /* set 80 ms */
        adxl345_window_convert_to_register(80.0f, (uint8_t *)&reg);
        /* set windows time */
        adxl345_set_window(reg);
        adxl345_get_window(&tap_window);
        printf("adxl345_get_window == 0x%02x\r\n",tap_window);

        /* set x axis */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_X, ADXL345_BOOL_FALSE);
        adxl345_get_tap_axis(ADXL345_TAP_AXIS_X,&reg);
        printf("adxl345_get_tap_x_axis == 0x%02x\r\n",reg);
  
        /* set y axis */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Y, ADXL345_BOOL_FALSE);
        adxl345_get_tap_axis(ADXL345_TAP_AXIS_Y,&reg);
        printf("adxl345_get_tap_y_axis == 0x%02x\r\n",reg);
 
        /* set z axis */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Z, ADXL345_BOOL_FALSE);
        adxl345_get_tap_axis(ADXL345_TAP_AXIS_Z,&reg);
        printf("adxl345_get_tap_z_axis == 0x%02x\r\n",reg);

        /* disable suppress */
        adxl345_set_tap_suppress(ADXL345_BOOL_FALSE);
        adxl345_get_tap_suppress(&reg);
        printf("adxl345_get_tap_suppress == 0x%02x\r\n",reg);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_SINGLE_TAP, ADXL345_INTERRUPT_PIN1);
        adxl345_get_interrupt_map(ADXL345_INTERRUPT_SINGLE_TAP, &reg);
        printf("adxl345_get_interrupt_map == 0x%02x\r\n",reg);

        /* set single tap */
        adxl345_set_interrupt(ADXL345_INTERRUPT_SINGLE_TAP, ADXL345_BOOL_FALSE);
        adxl345_get_interrupt(ADXL345_INTERRUPT_SINGLE_TAP,&reg);
        printf("adxl345_get_interrupt == 0x%02x\r\n",reg);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_DOUBLE_TAP, ADXL345_INTERRUPT_PIN1);
        adxl345_get_interrupt_map(ADXL345_INTERRUPT_DOUBLE_TAP, &reg);
        printf("adxl345_get_interrupt_map == 0x%02x\r\n",reg);

        /* set double tap */
        adxl345_set_interrupt(ADXL345_INTERRUPT_DOUBLE_TAP, ADXL345_BOOL_FALSE);
        adxl345_get_interrupt(ADXL345_INTERRUPT_DOUBLE_TAP,&reg);
        printf("adxl345_get_interrupt == 0x%02x\r\n",reg);

        /* link activity and inactivity */
        adxl345_set_link_activity_inactivity(ADXL345_BOOL_FALSE);
        adxl345_get_link_activity_inactivity(&reg);
        printf("adxl345_get_link_activity_inactivity == 0x%02x\r\n",reg);

        adxl345_action_threshold_convert_to_register(2.0f,(uint8_t *)&reg);
        adxl345_set_action_threshold(reg);
        adxl345_get_action_threshold(&action_threshold);
        printf("adxl345_get_action_threshold == 0x%02x\r\n",action_threshold);

        adxl345_inaction_threshold_convert_to_register(1.0f, (uint8_t *)&reg);
        /* set inaction threshold */
        adxl345_set_inaction_threshold( reg);
        adxl345_get_inaction_threshold(&inaction_threshold);
        printf("adxl345_get_inaction_threshold == 0x%02x\r\n",inaction_threshold);

        /* set 3 s */
        adxl345_inaction_time_convert_to_register(3, (uint8_t *)&reg); 
        /* set inaction time */
        adxl345_set_inaction_time(reg);
        adxl345_get_inaction_time(&inaction_time);
        printf("adxl345_get_inaction_time == 0x%02x\r\n",inaction_time);

        /* set action x */
        adxl345_set_action_inaction(ADXL345_ACTION_X, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_ACTION_X,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);

        /* set action y */
        adxl345_set_action_inaction(ADXL345_ACTION_Y, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_ACTION_Y,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);   
        
        /* set action z */
        adxl345_set_action_inaction(ADXL345_ACTION_Z, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_ACTION_Z,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);    
        
        /* set inaction x */
        adxl345_set_action_inaction(ADXL345_INACTION_X, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_INACTION_X,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);
        
        /* set inaction y */
        adxl345_set_action_inaction(ADXL345_INACTION_Y, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_INACTION_Y,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);    
        
        /* set inaction z */
        adxl345_set_action_inaction(ADXL345_INACTION_Z, ADXL345_BOOL_FALSE);
        adxl345_get_action_inaction(ADXL345_INACTION_Z,&reg);
        printf("adxl345_get_action_inaction == 0x%02x\r\n",reg);

        /* set action ac coupled */
        adxl345_set_action_coupled(ADXL345_COUPLED_AC);
        adxl345_get_action_coupled(&reg);
        printf("adxl345_get_action_coupled == 0x%02x\r\n",reg);

        /* set inaction dc coupled */
        adxl345_set_inaction_coupled(ADXL345_COUPLED_DC);
        adxl345_get_inaction_coupled(&reg);
        printf("adxl345_get_inaction_coupled == 0x%02x\r\n",reg);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_ACTIVITY, ADXL345_INTERRUPT_PIN1);
        /* set activity */
        adxl345_set_interrupt(ADXL345_INTERRUPT_ACTIVITY, ADXL345_BOOL_FALSE);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_INACTIVITY, ADXL345_INTERRUPT_PIN1);
        /* set inactivity */
        adxl345_set_interrupt(ADXL345_INTERRUPT_INACTIVITY, ADXL345_BOOL_FALSE);
   
        /* set free fall */
        adxl345_free_fall_threshold_convert_to_register( 0.8f, (uint8_t *)&reg);     
        /* set free fall threshold */
        adxl345_set_free_fall_threshold(reg);
        adxl345_get_free_fall_threshold(&fall_threshold);
        printf("adxl345_get_free_fall_threshold == 0x%02x\r\n",fall_threshold);

        /* set 10 ms */
        adxl345_free_fall_time_convert_to_register(10, (uint8_t *)&reg);
        /* set free fall time */
        adxl345_set_free_fall_time(reg);
        adxl345_get_free_fall_time(&free_fall_time);
        printf("adxl345_get_free_fall_time == 0x%02x\r\n",free_fall_time);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_FREE_FALL, ADXL345_INTERRUPT_PIN1);
   
        /* set fall */
        adxl345_set_interrupt(ADXL345_INTERRUPT_FREE_FALL, ADXL345_BOOL_FALSE);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_DATA_READY, ADXL345_INTERRUPT_PIN1);
        /* set data ready */
        adxl345_set_interrupt(ADXL345_INTERRUPT_DATA_READY, ADXL345_BOOL_FALSE);
   
        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_WATERMARK, ADXL345_INTERRUPT_PIN1);
        /* set water mark */
        adxl345_set_interrupt(ADXL345_INTERRUPT_WATERMARK, ADXL345_BOOL_FALSE);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_OVERRUN, ADXL345_INTERRUPT_PIN1);
        /* set over run */
        adxl345_set_interrupt(ADXL345_INTERRUPT_OVERRUN, ADXL345_BOOL_FALSE);

        /* set justify right */
        adxl345_set_justify(ADXL345_JUSTIFY_RIGHT);
        adxl345_get_justify(&reg);
        printf("adxl345_get_justify == 0x%02x\r\n",reg);
        /* set 100 rate */
        adxl345_set_rate(ADXL345_RATE_100);
        adxl345_get_rate(&reg);
        printf("adxl345_get_rate == 0x%02x\r\n",reg);

        /* set full resolution */
        adxl345_set_full_resolution(ADXL345_BOOL_TRUE);
        adxl345_get_full_resolution(&reg);
        printf("adxl345_get_full_resolution == 0x%02x\r\n",reg);
        /* 2g */
        adxl345_set_range(ADXL345_RANGE_4G);
        adxl345_get_range(&reg);
        printf("adxl345_get_range == 0x%02x\r\n",reg);

        /* start measure */
        adxl345_set_measure(ADXL345_BOOL_TRUE);
    
        adxl345_get_measure(&reg);
        printf("adxl345_get_measure == 0x%02x\r\n",reg);

        uint8_t xyz_data[6] = {0};
        uint16_t x_data;
        uint16_t y_data;
        uint16_t z_data;

        while(1)
        {
            adxl345_get_x_y_z_data(ADXL345_REG_DATAX0,xyz_data,6);
            x_data = (int16_t)(xyz_data[1] << 8) | xyz_data[0];                                              /* set raw z */
            y_data = (int16_t)(xyz_data[3] << 8) | xyz_data[2];                                              /* set raw y */
            z_data = (int16_t)(xyz_data[5] << 8) | xyz_data[4];  

            printf("adxl345_get_x_data == %0.2f g\r\n",(float)x_data * 0.0039f);
            printf("adxl345_get_y_data == %0.2f g\r\n",(float)y_data * 0.0039f);
            printf("adxl345_get_z_data == %0.2f g\r\n",(float)z_data * 0.0039f);

            delay_1ms(1000);
        }
    }  
}


static void init_gpio_pb1_exit_irq(void)
{
    //配置PB1外部中断
	rcu_periph_clock_enable(RCU_GPIOB);//GPIOB时钟使能
    gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_1);//PB1配置成上拉输入
	nvic_irq_enable(EXTI1_IRQn, 2U, 1U);//enable and set key EXTI interrupt to the lowest priority 
    gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_1);//connect key EXTI line to key GPIO pin
	exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//下降沿中断
	exti_interrupt_flag_clear(EXTI_1);//清中断标志
}

/**
 * 活动事件的初始化
*/
void adxl345_active_init()
{
    uint8_t id = 0;
    uint8_t reg;

    adxl345_get_device_id(&id);
    if (id == 0xe5)
    {
        adxl345_write_reg(ADXL345_REG_INT_ENABLE, 0x00);  //关掉中断
        delay_1ms(100);
        adxl345_set_full_resolution(ADXL345_BOOL_TRUE); 
        delay_1ms(100);  // 延时必须，不然有时候数据写不进去。
        adxl345_set_range(ADXL345_RANGE_16G);
        delay_1ms(100); 
        adxl345_set_justify(ADXL345_BOOL_FALSE); 
        delay_1ms(100);  
        adxl345_set_interrupt_active_level(ADXL345_INTERRUPT_ACTIVE_LEVEL_LOW);
        delay_1ms(100); 
 
        adxl345_set_rate(ADXL345_RATE_100);
        delay_1ms(100);

        adxl345_set_measure(ADXL345_BOOL_TRUE);
        delay_1ms(100);
        adxl345_set_auto_sleep(ADXL345_BOOL_FALSE);
        delay_1ms(100);                               // 设置自动休眠为关
        adxl345_set_sleep(ADXL345_BOOL_FALSE); 
        delay_1ms(100);        
        adxl345_set_sleep_frequency(ADXL345_SLEEP_FREQUENCY_1HZ);
        delay_1ms(100);
        adxl345_set_link_activity_inactivity(ADXL345_BOOL_FALSE); 
        delay_1ms(100);

        //set x y z offset
        adxl345_offset_convert_to_register(0.0f,&reg);
        adxl345_set_offset(reg,reg,reg);
        delay_1ms(100);
        /* set tap */
        adxl345_tap_threshold_convert_to_register(2.0f, (uint8_t *)&reg);
        adxl345_set_tap_threshold(reg);
        
        /* set duration time */
        adxl345_duration_convert_to_register(10*1000, (uint8_t *)&reg);
        adxl345_set_duration(reg);
    
        /* set latent time */
        adxl345_latent_convert_to_register(20.0f, (uint8_t *)&reg);
        adxl345_set_latent(reg);

        /* set window */
        adxl345_window_convert_to_register(80.0f, (uint8_t *)&reg);
        adxl345_set_window(reg);

        /* set tap x */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_X, ADXL345_BOOL_TRUE);
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Y, ADXL345_BOOL_TRUE);
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Z, ADXL345_BOOL_TRUE);
        
        adxl345_action_threshold_convert_to_register(2.0f,(uint8_t *)&reg);
        adxl345_set_action_threshold(reg);  

        adxl345_inaction_threshold_convert_to_register(1.0f, (uint8_t *)&reg);
        /* set inaction threshold */
        adxl345_set_inaction_threshold( reg);           

        /* set 3 s */
        adxl345_inaction_time_convert_to_register(3, (uint8_t *)&reg); 
        /* set inaction time */
        adxl345_set_inaction_time(reg);

        /* set action x */
        adxl345_set_action_inaction(ADXL345_ACTION_X, ADXL345_BOOL_TRUE);
 
        /* set action y */
        adxl345_set_action_inaction(ADXL345_ACTION_Y, ADXL345_BOOL_TRUE);

        /* set action z */
        adxl345_set_action_inaction(ADXL345_ACTION_Z, ADXL345_BOOL_TRUE);
 
        /* set inaction x */
        adxl345_set_action_inaction(ADXL345_INACTION_X, ADXL345_BOOL_TRUE);

        /* set inaction y */
        adxl345_set_action_inaction(ADXL345_INACTION_Y, ADXL345_BOOL_TRUE);
        
        /* set inaction z */
        adxl345_set_action_inaction(ADXL345_INACTION_Z, ADXL345_BOOL_TRUE);
  
        /* set action ac coupled */
        adxl345_set_action_coupled(ADXL345_COUPLED_AC);
    
        /* set inaction dc coupled */
        adxl345_set_inaction_coupled(ADXL345_COUPLED_DC);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_ACTIVITY, ADXL345_INTERRUPT_PIN1);
        /* set activity */
        adxl345_set_interrupt(ADXL345_INTERRUPT_ACTIVITY, ADXL345_BOOL_TRUE);
        delay_1ms(200);  
        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_INACTIVITY, ADXL345_INTERRUPT_PIN1);
        /* set inactivity */
        adxl345_set_interrupt(ADXL345_INTERRUPT_INACTIVITY, ADXL345_BOOL_TRUE);
        printf("Adxl345 init Success\r\n");

        adxl345_read_reg(ADXL345_REG_INT_ENABLE,&reg);

        printf("ADXL345_REG_INT_ENABLE == %x\r\n",reg);
        init_gpio_pb1_exit_irq();

        while(1)
        {

        }

    }  
}

/**
 * 敲击事件
*/
void adxl345_tap_init()
{
    uint8_t id = 0;
    uint8_t reg;

    adxl345_get_device_id(&id);
    if (id == 0xe5)
    {
        adxl345_write_reg(ADXL345_REG_INT_ENABLE, 0x00);  //关掉中断
        delay_1ms(100);
        adxl345_set_full_resolution(ADXL345_BOOL_TRUE); 
        delay_1ms(100);  // 延时必须，不然有时候数据写不进去。
        adxl345_set_range(ADXL345_RANGE_4G);
        delay_1ms(100); 
        adxl345_set_justify(ADXL345_BOOL_FALSE); 
        delay_1ms(100);  
        adxl345_set_interrupt_active_level(ADXL345_INTERRUPT_ACTIVE_LEVEL_LOW);
        delay_1ms(100); 
 
        adxl345_set_rate(ADXL345_RATE_100);
        delay_1ms(100);

        adxl345_set_measure(ADXL345_BOOL_TRUE);
        delay_1ms(100);
        adxl345_set_auto_sleep(ADXL345_BOOL_FALSE);
        delay_1ms(100);                               // 设置自动休眠为关
        adxl345_set_sleep(ADXL345_BOOL_FALSE); 
        delay_1ms(100);        
        adxl345_set_sleep_frequency(ADXL345_SLEEP_FREQUENCY_1HZ);
        delay_1ms(100);
        adxl345_set_link_activity_inactivity(ADXL345_BOOL_FALSE); 
        delay_1ms(100);

        //set x y z offset
        adxl345_offset_convert_to_register(0.0f,&reg);
        adxl345_set_offset(reg,reg,reg);
        delay_1ms(100);

        /* set tap */
        adxl345_tap_threshold_convert_to_register(4.0f, (uint8_t *)&reg);
        adxl345_set_tap_threshold(reg);
        
        /* set duration time */
        adxl345_duration_convert_to_register(30*1000, (uint8_t *)&reg);
        adxl345_set_duration(reg);
    
        /* set latent time */
        adxl345_latent_convert_to_register(42.0f, (uint8_t *)&reg);
        adxl345_set_latent(reg);

        /* set window */
        adxl345_window_convert_to_register(200.0f, (uint8_t *)&reg);
        adxl345_set_window(reg);

        adxl345_read_reg(ADXL345_REG_WINDOW,&reg);

        printf("ADXL345_REG_WINDOW == %x\r\n",reg);

        /* set tap x */
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_X, ADXL345_BOOL_FALSE);
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Y, ADXL345_BOOL_FALSE);
        adxl345_set_tap_axis(ADXL345_TAP_AXIS_Z, ADXL345_BOOL_TRUE);

        adxl345_write_reg(ADXL345_REG_THRESH_TAP ,50); 
        adxl345_write_reg(ADXL345_REG_DUR ,15); 
        adxl345_write_reg(ADXL345_REG_LATENT ,80); 
        adxl345_write_reg(ADXL345_REG_WINDOW ,200); 

        /* set free fall */
        adxl345_free_fall_threshold_convert_to_register( 0.8f, (uint8_t *)&reg);     
        /* set free fall threshold */
        adxl345_set_free_fall_threshold(reg);

        /* set 10 ms */
        adxl345_free_fall_time_convert_to_register(30, (uint8_t *)&reg);
        /* set free fall time */
        adxl345_set_free_fall_time(reg);


        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_SINGLE_TAP, ADXL345_INTERRUPT_PIN1);

        /* set single tap */
        adxl345_set_interrupt(ADXL345_INTERRUPT_SINGLE_TAP, ADXL345_BOOL_TRUE);

        /* map int 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_DOUBLE_TAP, ADXL345_INTERRUPT_PIN1);

        /* set double tap */
        adxl345_set_interrupt(ADXL345_INTERRUPT_DOUBLE_TAP, ADXL345_BOOL_TRUE);

        /* set interrupt 1 */
        adxl345_set_interrupt_map(ADXL345_INTERRUPT_FREE_FALL, ADXL345_INTERRUPT_PIN1);
   
        /* set fall */
        adxl345_set_interrupt(ADXL345_INTERRUPT_FREE_FALL, ADXL345_BOOL_TRUE);

        adxl345_read_reg(ADXL345_REG_INT_ENABLE,&reg);
        printf("ADXL345_REG_INT_ENABLE == %x\r\n",reg);

        adxl345_read_reg(ADXL345_REG_TAP_AXES,&reg);
        printf("ADXL345_REG_TAP_AXES == %x\r\n",reg);
        init_gpio_pb1_exit_irq();

        while(1)
        {

        }

    }  
}


void hander_message(void)
{
    int value = 0;
    adxl345_read_reg(ADXL345_REG_INT_SOURCE,&value);
   
    if (value & 0x10)
    {
        printf("Adxl345 is Active\r\n");
    }
    else if (value & 0x08)
    {
        printf("Adxl345 is InActive\r\n");
    }
    else if (value & 0x40)
    {
        printf("Adxl345 single Tap\r\n");
    }
    else if (value & 0x20)
    {
        printf("Adxl345 double Tap\r\n");
    }
    else if (value & 0x08)
    {
        printf("Adxl345 free fall\r\n");
    }
}